package com.cxs.musicplaywidget.slice;

import com.cxs.musicplaywidget.ResourceTable;
import com.cxs.musicplaywidget.commonevent.EventManager;
import com.cxs.musicplaywidget.data.EventParam;
import com.cxs.musicplaywidget.data.MDevice;
import com.cxs.musicplaywidget.service.DeviceManager;
import com.cxs.musicplaywidget.service.ServiceManager;
import com.cxs.musicplaywidget.util.LogUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.TabList;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.bundle.BundleInfo;
import ohos.rpc.RemoteException;
import ohos.utils.zson.ZSONObject;

import java.util.List;

import static com.cxs.musicplaywidget.data.EventParam.EventAction.*;

public class MainAbilitySlice extends AbilitySlice {
    private static final String tag = MainAbilitySlice.class.getSimpleName();
    private static boolean isLoaded = false;
    private static byte curDeviceIndex;
    private static byte newDeviceIndex;
    private TabList deviceTabList;
    DeviceManager deviceManager;
    ServiceManager serviceManager;
    TaskDispatcher globalTaskDispatcher;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        try {
            BundleInfo bundleInfo = getBundleManager().getBundleInfo(getBundleName(), 0);
            System.out.println("ZSONObject.toZSONString(bundleInfo) = " + ZSONObject.toZSONString(bundleInfo));
        } catch (RemoteException e) {
            e.printStackTrace();
        }

        initManager();
//        initComponent();

    }


    public void initManager() {
        LogUtils.debug(tag, "init()");
        EventManager.subscribe(this);
        deviceManager = DeviceManager.getInstance(getContext());
        List<MDevice> deviceList = deviceManager.getDeviceList();
        serviceManager = ServiceManager.getInstance(this);
        if (!deviceList.isEmpty()) {
            EventManager.publishEvent(new EventParam(LOAD_DEVICE_LIST));
            if (!isLoaded) {
                serviceManager.init();
                isLoaded = true;
            }
        }
//        globalTaskDispatcher = getApplicationContext().getGlobalTaskDispatcher(TaskPriority.LOW);
//        globalTaskDispatcher.asyncDispatch(() -> {
//
//        });

    }

    private void initComponent() {
        deviceTabList = findComponentById(ResourceTable.Id_device_tab_list);

        deviceTabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                LogUtils.debug(tag, "onSelected.tab = " +tab.getPosition());
                LogUtils.debug(tag, "onSelected.newDeviceIndex = " +newDeviceIndex);
                LogUtils.debug(tag, "onSelected.curDeviceIndex = " +curDeviceIndex);
                newDeviceIndex = (byte) tab.getPosition();

                boolean connected = serviceManager.connectService(newDeviceIndex);
                if (connected) {
                    curDeviceIndex = newDeviceIndex;
                }
            }
            @Override
            public void onUnselected(TabList.Tab tab) {}
            @Override
            public void onReselected(TabList.Tab tab) {}
        });
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        LogUtils.debug(tag, "onInactive()");
        EventManager.setIsAbilitySubscribe(false);
    }

    @Override
    protected void onStop() {
        super.onStop();
        LogUtils.debug(tag, "onStop()");
    }
}
